package ${package}<#if moduleName??>.${moduleName}</#if>.controller<#if subModuleName??>.${subModuleName}</#if>.admin;

import ${package}<#if moduleName??>.${moduleName}</#if>.pojo.entity.${ClassName};
import ${package}<#if moduleName??>.${moduleName}</#if>.service.I${ClassName}Service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.*;



/**
* ${tableComment}
*
* @author ${author} ${email}
* @since ${version} ${date}
*/

@Api(tags = "${tableComment}")
@RestController
@RequestMapping("/api/v1/${classname}")
@RequiredArgsConstructor
public class ${ClassName}Controller {

    private final I${ClassName}Service i${ClassName}Service;

    @ApiOperation(value = "列表分页")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", dataType = "Integer"),
    @ApiImplicitParam(name = "pageSize", value = "每页数量", paramType = "query", dataType = "Integer"),
<#if queryList??>
<#list queryList as queryItem>
    @ApiImplicitParam(name = "${queryItem.attrName}", value = "<#if queryItem.columnComment!?length gt 0>${queryItem.columnComment}</#if>", paramType = "query", dataType = "${queryItem.attrType}"),
</#list>
</#if>
    })
    @GetMapping("/page")
    public PageResult<${ClassName}> list(Long pageNum, Long pageSize, <#if queryList??><#list queryList as queryItem>
    <#if queryItem.formType == 'date'>@DateTimeFormat(pattern = "yyyy-MM-dd") <#elseif queryItem.formType == 'datetime'> DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") </#if>${queryItem.attrType} ${queryItem.attrName}<#sep>,</#list></#if>) {

        Page<${ClassName}> result = i${ClassName}Service.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<${ClassName}>()
        <#if queryList??>
        <#list queryList as queryItem>
            <#if queryItem.queryType = '='>
            .eq(${queryItem.attrName} != null,${ClassName}::${queryItem.queryMethodName},${queryItem.attrName})
            <#else>
            .like(StrUtil.isNotBlank(${queryItem.attrName}), ${ClassName}::${queryItem.queryMethodName}, StrUtil.trimToNull(${queryItem.attrName}))
            </#if>
        </#list>
            //.orderByDesc(${ClassName}::getGmtModified) //排序样例
            );
        </#if>

        return PageResult.success(result);

     }

    @ApiOperation(value = "列表")
    @GetMapping
    public Result list() {
        List<${ClassName}> list = i${ClassName}Service.list(new LambdaQueryWrapper<${ClassName}>()
            //.orderByDesc(${ClassName}::getGmtModified)
        );
        return Result.success(list);
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        ${ClassName} ${className} = i${ClassName}Service.getById(id);
        return Result.success(${className});
    }

    @ApiOperation(value = "新增")
    @ApiImplicitParam(name = "${className}", value = "实体JSON对象", required = true, paramType = "body", dataType = "${ClassName}")
    @PostMapping
    public Result add(@RequestBody ${ClassName} ${className}) {
        boolean status = i${ClassName}Service.save(${className});
        return Result.judge(status);
    }

    @ApiOperation(value = "修改")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long"),
    @ApiImplicitParam(name = "${ClassName}", value = "实体JSON对象", required = true, paramType = "body", dataType = "${ClassName}")
    })
    @PutMapping(value = "/{id}")
    public Result update(
    @PathVariable Long id,
    @RequestBody ${ClassName} ${className}) {
        boolean status = i${ClassName}Service.updateById(${className});
        return Result.judge(status);
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "ids", value = "以,分割拼接字符串", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        boolean status = i${ClassName}Service.removeByIds(idList);
        return Result.judge(status);
    }

}

